/*
 * @Author: scl
 * @Date: 2023-09-01 22:55:35
 * @LastEditTime: 2023-09-01 23:19:41
 * @Description: file content
 */
/*
 * @lc app=leetcode.cn id=59 lang=typescript
 *
 * [59] 螺旋矩阵 II
 *
 * https://leetcode.cn/problems/spiral-matrix-ii/description/
 *
 * algorithms
 * Medium (72.27%)
 * Likes:    1139
 * Dislikes: 0
 * Total Accepted:    347.1K
 * Total Submissions: 480.2K
 * Testcase Example:  '3'
 *
 * 给你一个正整数 n ，生成一个包含 1 到 n^2 所有元素，且元素按顺时针顺序螺旋排列的 n x n 正方形矩阵 matrix 。
 * 
 * 
 * 
 * 示例 1：
 * 
 * 
 * 输入：n = 3
 * 输出：[[1,2,3],[8,9,4],[7,6,5]]
 * 
 * 
 * 示例 2：
 * 
 * 
 * 输入：n = 1
 * 输出：[[1]]
 * 
 * 
 * 
 * 
 * 提示：
 * 
 * 
 * 1 
 * 
 * 
 */

// @lc code=start
function generateMatrix(n: number): number[][] {
    const result:number[][]=Array.from(Array(n),x=>new Array(n))
    let m=n-1,k=0,num=0
    while(num<n*n){
        for (let i = k; i <= m - k; i++)result[k][i]=++num
        for (let i = k + 1; i <= m - k; i++)result[i][m-k]=++num
        for (let i = m - k-1 ; i >= k; i--)result[m-k][i]=++num
        for (let i = m - k - 1; i >= k + 1; i--)result[i][k]=++num
        k++
    }
    return result
};
// @lc code=end

